﻿using AutoMapper.Internal.Mappers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS;
using NPOI.SS.UserModel;
using NPOI.XSSF;    
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

using ThridGroup.ERP.DTO.JobGradeManagementDTO; 
using ThridGroup.ERP.HumanResources;
namespace ThridGroup.ERP.Web.Controllers
{
    public class HumanResourcesController : Controller
    {
        /// <summary>
        /// 职级管理接口
        /// </summary>
        private readonly IPositionManagementServices _IPositionManagementServices;
        /// <summary>
        /// 直接管理构造方法
        /// </summary>
        /// <param name="iPositionManagementServices"></param>
        public HumanResourcesController(IPositionManagementServices iPositionManagementServices)
        {
            _IPositionManagementServices = iPositionManagementServices;
        }

        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        /// <summary>
        /// 添加职位管理
        /// </summary>
        /// <param name="jobGradeManagementModel"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddJobGradeManagementModel(JobGradeManagementModelDTO jobGradeManagementModel)
        {
            var i = await _IPositionManagementServices.AddJobGradeManagementModel(jobGradeManagementModel);
            return i;
        }

        /// <summary>
        /// 职位等级视图
        /// </summary>
        /// <returns></returns>
        public IActionResult JobGradeManagement()
        {
            return View();
        }

        /// <summary>
        /// 显示职位信息
        /// </summary>
        /// <returns></returns>

        [HttpGet]
        public async Task<JsonResult> GetJobGradeTableData([FromQuery] QueryJobGradeManagementDTO queryJobGradeManagementDTO)
        {
            var list = await _IPositionManagementServices.QueryJobGradeManagement(queryJobGradeManagementDTO);
         var   jobGradeManagementModels = list.OrderByDescending(a => a.Id).Skip((queryJobGradeManagementDTO.PageIndex - 1) * queryJobGradeManagementDTO.PageSize).Take(queryJobGradeManagementDTO.PageSize).ToList();

            return Json(new
            {
                code = 0,
                msg = "",
                count = list.Count, // 如果有分页，这里应为总条数
                data = jobGradeManagementModels
            });
        }

        [HttpGet]
        /// <summary>
        /// 回显单条职位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<JobGradeManagementModelDTO> GetJobGradeManagementModel(int id)
        {
            return await _IPositionManagementServices.GetJobGradeManagementModel(id);
        }
        [HttpDelete]
        /// <summary>
        ///删除职位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DelJobGradeManagement(int id)
        {

            await _IPositionManagementServices.DelJobGradeManagement(id);
        }
        [HttpPost]
        /// <summary>
        /// 批量删除职位信息
        /// </summary>
        /// <param name="ids">要删除的职位ID列表</param>
        /// <returns></returns>
        public async Task BatchDelJobGradeManagement([FromBody] List<int> ids)
        {
            await _IPositionManagementServices.BatchDelJobGradeManagement(ids);
        }



        [HttpPut]
        /// <summary>
        /// 修改职位信息
        /// </summary>
        /// <returns></returns>
        public async Task<int> UpJobGradeManagement(JobGradeManagementModelDTO jobGradeManagementModelDTO)
        {
            return await _IPositionManagementServices.UpJobGradeManagement(jobGradeManagementModelDTO);
        }


        //导出
        /// <summary>
        /// 导出操作
        /// </summary>
        /// <returns></returns>
      [HttpGet]
public async Task<IActionResult> ExportExcel([FromQuery] QueryJobGradeManagementDTO query, bool exportAll = false)
{
    try
    {
        byte[] data = null;
        string t = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
        string f = "导出职级管理信息.xlsx";

        // 新建工作簿
        IWorkbook b = new XSSFWorkbook();
        ISheet s = b.CreateSheet();
        IRow row1 = s.CreateRow(0);

        // 设置表头
        row1.CreateCell(0).SetCellValue("职级编码");
        row1.CreateCell(1).SetCellValue("职级层次");
        row1.CreateCell(2).SetCellValue("职级层次名称");
        row1.CreateCell(3).SetCellValue("薪资范围下限");
        row1.CreateCell(4).SetCellValue("薪资范围上限");
        row1.CreateCell(5).SetCellValue("备注");

        // 获取数据
        var list = await _IPositionManagementServices.QueryJobGradeManagement(query);

        // 判断是否导出全部
        var exportList = exportAll
            ? list.OrderByDescending(a => a.Id).ToList()
            : list.OrderByDescending(a => a.Id)
                  .Skip((query.PageIndex - 1) * query.PageSize)
                  .Take(query.PageSize)
                  .ToList();

        int index = 1;
        foreach (var item in exportList)
        {
            IRow row = s.CreateRow(index);
            row.CreateCell(0).SetCellValue(item.JobGradeCoding);
            row.CreateCell(1).SetCellValue(item.JobGradeLevel);
            row.CreateCell(2).SetCellValue(item.JobGradeLevelName);
            row.CreateCell(3).SetCellValue(item.SalaryRangelower != null ? item.SalaryRangelower.ToString() : "");
            row.CreateCell(4).SetCellValue(item.SalaryRangelimits != null ? item.SalaryRangelimits.ToString() : "");
            row.CreateCell(5).SetCellValue(item.ReferenceColumn ?? "");
            index++;
        }

        using (MemoryStream ms = new MemoryStream())
        {
            b.Write(ms);
            data = ms.ToArray();
        }

        return File(data, t, f);
    }
    catch (Exception ex)
    {
        return Content("导出失败：" + ex.Message);
    }
}


    }
}
